package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.common.constant.HostRealTimeLocationRedisKey;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * 主播实时位置Redis DAO
 * @author xuanqicheng
 */
@Repository
public class HostRealTimeLocationRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 设置省份信息
     * @param userId
     * @param province
     */
    public void setProvince(long userId,String province){
        if(StringUtils.isNotBlank(province)){
            String key = HostRealTimeLocationRedisKey.getHashKey(userId);
            String provinceField = HostRealTimeLocationRedisKey.getProvinceField();
            stringRedisTemplate.opsForHash().put(key,provinceField,province);
            stringRedisTemplate.expire(key,4, TimeUnit.HOURS);
        }
    }

    /**l
     * 获取省份信息
     * @param userId
     * @return
     */
    public String getProvince(long userId){
        String key = HostRealTimeLocationRedisKey.getHashKey(userId);
        String provinceFiled = HostRealTimeLocationRedisKey.getProvinceField();
        String province = (String) stringRedisTemplate.opsForHash().get(key,provinceFiled);
        if(StringUtils.isNotBlank(province)){
            stringRedisTemplate.expire(key,4, TimeUnit.HOURS);

        }
        return province;
    }

    /**
     * 设置城市信息
     * @param userId
     * @param city
     */
    public void setCity(long userId,String city){
        if(StringUtils.isNotBlank(city)){
            String key = HostRealTimeLocationRedisKey.getHashKey(userId);
            String cityField = HostRealTimeLocationRedisKey.getCityField();
            stringRedisTemplate.opsForHash().put(key,cityField,city);
            stringRedisTemplate.expire(key,4,TimeUnit.HOURS);
        }
    }

    /**
     * 获取城市信息
     * @param userId
     * @return
     */
    public String getCity(long userId){
        String key = HostRealTimeLocationRedisKey.getHashKey(userId);
        String cityField = HostRealTimeLocationRedisKey.getCityField();
        String city = (String) stringRedisTemplate.opsForHash().get(key,cityField);
        if(StringUtils.isNotBlank(city)){
            stringRedisTemplate.expire(key,4, TimeUnit.HOURS);

        }

        return city;
    }

    /**
     * 设置经纬度,格式：lat,lon
     * 结构为：纬度(lat),经度(lon)
     * @param userId
     * @param location
     */
    public void setLocation(long userId,String location){
        if(StringUtils.isNotBlank(location)){
            String key = HostRealTimeLocationRedisKey.getHashKey(userId);
            String locationField = HostRealTimeLocationRedisKey.getLocationField();
            stringRedisTemplate.opsForHash().put(key,locationField,location);
            stringRedisTemplate.expire(key,4, TimeUnit.HOURS);
        }
    }

    /**
     * 获取经纬度,格式：lat,lon
     * 结构为：纬度(lat),经度(lon)
     * @param userId
     * @return  纬度(lat),经度(lon)
     */
    public String getLocation(long userId){
        String key = HostRealTimeLocationRedisKey.getHashKey(userId);
        String locationField = HostRealTimeLocationRedisKey.getLocationField();
        String location = (String) stringRedisTemplate.opsForHash().get(key,locationField);
        if(StringUtils.isNotBlank(location)){
            stringRedisTemplate.expire(key,4, TimeUnit.HOURS);
        }
        return location;
    }

    public void setAll(long userId,String location,String province,String city){
        setLocation(userId,location);
        setProvince(userId,province);
        setCity(userId,city);
    }

}
